import * as Vibrant from 'node-vibrant/dist/vibrant.worker.min.js';
import Color from 'color';
import dayjs from 'dayjs';
require('dayjs/locale/zh-cn')


// 实现动态获取图片大小
export const formatUrlSize = (url: string, width: number, height?: number): any => {
  if (!url) return
  height = width
  if (url.endsWith("&")) {
    const newUrl = url.replace("thumbnail=140y140&", "")
    return `${newUrl}thumbnail=${width}x${height}`
  } else {
    return `${url}?param=${width}y${height}`
  }
}
// 格式化播放量
export const formatPlayCount = (playcount: number) => {
  if (playcount > 10000) {
    return Math.floor((playcount / 10000)).toFixed(0) + "万"
  } else if (playcount > 100000000) {
    return Math.floor((playcount / 100000000)).toFixed(0) + "亿"
  } else {
    return playcount
  }
}
// 格式化时间
export const formatTime = (time: number) => {
  let minute = 0
  let seconds = 0

  minute = Math.floor(time / 60);
  seconds = Math.floor((time) % 60);
  const formatM = minute.toString().padStart(2, "0");
  const formatS = seconds.toString().padStart(2, "0");
  return `${formatM}:${formatS}`;
}
// 自动读取图片中的颜色
export const formatBgColor = async (imgUrl: string) => {
  let background = ""
  const palette = await Vibrant.from(imgUrl).getPalette()
  const originColor = Color.rgb(palette.DarkMuted._rgb);
  const color = originColor.darken(0.1).rgb().string();
  const color2 = originColor.lighten(0.28).rotate(-30).rgb().string();
  background = `linear-gradient(to top left, ${color}, ${color2})`;
  return background
}

// 格式化创建时间
export const formatCreateTime = (time: number, timeType = "YYYY-MM-DD") => {
  return dayjs(time).format(timeType)
}
